(function (angular) {
    'use strict';
    angular.module('fs.router', ['fs.common', 'ui.router', 'oc.lazyLoad']).run(main);

    main.$inject = ['$rootScope', 'loggerFactory'];
    function main($rootScope, loggerFactory) {
        var log = loggerFactory.getLogger('router');
        $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
            //log.debug(event.name + ': go[' + toState.name + ',' + toParams.params + '], from[' + fromState.name + ',' + angular.toJson(fromParams.params) + ']');
        });

        $rootScope.$on('$stateNotFound', function (event, unfoundState, fromState, fromParams) {
            log.error(event.name + ': go[' + unfoundState.to + '], from[' + fromState.name + ',' + angular.toJson(fromParams.params) + ']');
        });

        $rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
            $rootScope.pageId = toState.name ? toState.name.split('_').join('.') : '';
            try { $rootScope.buildNav($rootScope.pageId); } catch (e) { }
            try { $rootScope.removePageDescription(); } catch (e) { }
            try { $rootScope.removeFormState(); } catch (e) { }
            try { $rootScope.saveAuditAndNoteParams(toState.name, toParams); } catch (e) { }
            try { $rootScope.saveHistory(event, toState, toParams, fromState, fromParams); } catch (e) { }
            //log.debug(event.name + ': go[' + toState.name + ',' + angular.toJson(toParams.params) + '], from[' + fromState.name + ',' + angular.toJson(fromParams.params) + ']');
        });

        $rootScope.$on('$stateChangeError', function (event, toState, toParams, fromState, fromParams, error) {
            log.error(event.name + ': go[' + toState.name + ',' + angular.toJson(toParams.params) + '], from[' + fromState.name + ',' + angular.toJson(fromParams.params) + ']: ' + error);
        });
    }
})(angular);